/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.model.Category;
import com.techshop.model.News;
import com.techshop.service.CategoryService;
import com.techshop.service.NewsService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author hai
 */
@ManagedBean(name="newsController")
@ViewScoped
public class NewsController extends AbstractController{
    private News selectnews;
    List<Category> listCategory=new ArrayList();   
        private String filePath = "";

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    @ManagedProperty(value = "#{newsService}")
    NewsService newsService;
    
     @ManagedProperty(value = "#{categoryService}")
     CategoryService categoryService;

     @PostConstruct
     public void init()
     {
         selectnews = new News();
         selectnews.setCategory(new Category());
         selectnews.setDatePost(Calendar.getInstance().getTime());
         selectnews.setThumb("http://cdn.tgdd.vn/Files/2013/12/11/523956/smartphonegrades_primary-100155539-large-1-1-600x400.jpg");
         selectnews.setUserPost("Nguyễn Hải");
     }
     
    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
     public List<Category> getListCategory() {
        setListCategory(getCategoryService().findAll());
        return listCategory;
    }

    public void setListCategory(List<Category> listCategory) {
        this.listCategory = listCategory;
    }
    public News getSelectnews() {
        return selectnews;
    }

    public void setSelectnews(News selectnews) {
        this.selectnews = selectnews;
    }

    

    public NewsService getNewsService() {
        return newsService;
    }

    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    private List<News> listnews;
    public List<News> getListnews() {
        return getNewsService().findAll();
    }

    public void setListnews(List<News> listnews) {
        this.listnews = listnews;
    }
    
public void update()
    {
      
        if (getNewsService().update(getSelectnews()))
        {
            addInfoMessage("Cập nhật thành công");
        }
        else
        {
            addErrorMessage("Cập nhật thất bại");
        }
    }
    
   
    public void resetSelectNews(){
        init();
    }
    public void btnCreate(){
        
        selectnews.setThumb(filePath);
        if(getNewsService().insert(selectnews)){
            addInfoMessage("Thêm nhà sản xuất thành công");
        }else{
            addErrorMessage("Thêm nhà sản xuất không thành công");
        }
    }
//    
//    
//    public void btnUpdate(){
//        if(getCategoryService().update(getSelectcategory())){
//            addInfoMessage("Cập nhật nhà sản xuất thành công");
//        }else{
//            addErrorMessage("Cập nhật nhà sản xuất không thành công");
//        }
//    }
//    
//    
    public void btnDelete(){
        if(getNewsService().delete(getSelectnews())){
            addInfoMessage("Xóa  thành công");
        }else{
            addErrorMessage("Xóa  không thành công");
        }
    }
    
     public void handleFileUpload(FileUploadEvent event) throws IOException
    {
        //Primitives
        final int BUFFER_SIZE = 6124;
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        // Get categoryId
        String folderToUpload = "//resources//images//news//";
        (new File(extContext.getRealPath(folderToUpload))).mkdirs();       

        
        filePath = folderToUpload + event.getFile().getFileName();
        File result = new File(extContext.getRealPath(filePath));
        System.out.println(extContext.getRealPath(filePath));

        try
        {
            InputStream inputStream;
            try (FileOutputStream fileOutputStream = new FileOutputStream(result))
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int bulk;
                inputStream = event.getFile().getInputstream();
                while (true)
                {
                    bulk = inputStream.read(buffer);
                    if (bulk < 0)
                    {
                        break;
                    }
                    fileOutputStream.write(buffer, 0, bulk);
                    fileOutputStream.flush();
                }
            }
            inputStream.close();

            addInfoMessage("file name: " + event.getFile().getFileName() + 
                    "file size: " + event.getFile().getSize() / 1024 + " Kb content type: " + 
                    event.getFile().getContentType() + "The file was uploaded.");
            
            filePath = "//resources//images//news//" + event.getFile().getFileName();
        }
        catch (IOException e)
        {
            System.out.printf("Error :", e.toString());
            addErrorMessage("The files were not uploaded!");
        }
    }
   
    
    
    
}
